package com.longrise.android.widget.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;

import com.longrise.android.FrameworkManager;

public class LProgressView extends View
{
	private Context _context = null;
	private float _density = 1f;
	
	private Paint _paint = null;
	private Path _path = null;
	private float _strokeWidth = 0;
	private int _backColor = 0;
	private int _color = 0;
	
	private RectF _rect = null;
	private float _radiusLeftTop = 0;
	private float _radiusTopRight = 0;
	private float _radiusRightBottom = 0;
	private float _radiusLeftBottom = 0;
	
	private int _progress = 0;
	
	private void init()
	{
		try
		{
			super.setBackgroundColor(0x00000000);
			this._backColor = Color.rgb(248, 248, 248);
			this._color = Color.rgb(107, 207, 83);
			this._density = FrameworkManager.getInstance().getDensity();
			this._strokeWidth = 1*this._density;
			
			this._paint = new Paint();
			if(null!=this._paint)
			{
				this._paint.setAntiAlias(true);
				this._paint.setStyle(Paint.Style.FILL);				
			}			
			this._rect = new RectF();
			this._path = new Path();
		}
		catch(Exception ex){}
		finally
		{
			
		}
	}
	
	private void initPaint()
	{
		try
		{
			if(null!=this._paint)
			{
				this._paint.setStrokeWidth(this._strokeWidth);
			}
			
			
		}
		catch(Exception ex){}
		finally
		{
			
		}
		
	}
	
	public void setColor(int color)
	{
		this._color = color;
	}
	
	@Override
	public void setBackgroundColor(int color) 
	{
		this._backColor = color;
	}
	
	public void setProgress(int progress)
	{
		if(this._progress != progress)
		{
			this._progress = progress;
			
			this.invalidate();
		}
	}
	
	
	/**
	 * 设置圆角半径
	 * @param radius
	 */
	public void setFilletRadius(float radius)
	{
		this._radiusLeftTop = radius*this._density;
		this._radiusTopRight = radius*this._density;
		this._radiusRightBottom = radius*this._density;
		this._radiusLeftBottom = radius*this._density;
	}
	
	public void setFilletRadius(float leftTop, float topRight, float rightBottom, float leftBottom)
	{
		this._radiusLeftTop = leftTop*this._density;
		this._radiusTopRight = topRight*this._density;
		this._radiusRightBottom = rightBottom*this._density;
		this._radiusLeftBottom = leftBottom*this._density;
	}
	
	public void setFilletRadiusLeftTop(float radius)
	{
		this._radiusLeftTop = radius*this._density;
	}
	
	public void setFilletRadiusTopRight(float radius)
	{
		this._radiusTopRight = radius*this._density;
	}
	
	public void setFilletRadiusRightBottom(float radius)
	{
		this._radiusRightBottom = radius*this._density;
	}
	
	public void setFilletRadiusLeftBottom(float radius)
	{
		this._radiusLeftBottom = radius*this._density;
	}
	
	public LProgressView(Context context) 
	{
		super(context);
		this.init();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
	{
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		this.initPaint();
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		try
		{
			super.onDraw(canvas);
			
			if(null!=this._rect)
			{
				//draw background
				this._rect.set(this._strokeWidth/2, this._strokeWidth/2, this.getWidth()-this._strokeWidth/2, this.getHeight()-this._strokeWidth/2);				
				if(null!=this._path)
				{
					this._path.reset();
					this._path.addRoundRect(this._rect, new float[]{this._radiusLeftTop,this._radiusLeftTop,this._radiusTopRight,this._radiusTopRight,this._radiusRightBottom,this._radiusRightBottom,this._radiusLeftBottom,this._radiusLeftBottom}, Path.Direction.CW);
					if(null!=this._paint)
					{
						this._paint.setColor(this._backColor);
						canvas.drawPath(this._path, this._paint);
					}
				}
				
				//draw progress
				this._rect.set(this._strokeWidth/2, this._strokeWidth/2, (this._progress * this.getWidth() / 100), this.getHeight()-this._strokeWidth/2);				
				if(null!=this._path)
				{
					this._path.reset();
					this._path.addRoundRect(this._rect, new float[]{this._radiusLeftTop,this._radiusLeftTop,this._radiusTopRight,this._radiusTopRight,this._radiusRightBottom,this._radiusRightBottom,this._radiusLeftBottom,this._radiusLeftBottom}, Path.Direction.CW);
					if(null!=this._paint)
					{
						this._paint.setColor(this._color);
						canvas.drawPath(this._path, this._paint);
					}
				}
				
			}
			
		}
		catch(Exception ex){}
		finally
		{
			canvas = null;
		}
	}

}
